$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'myCourse/choosetest/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '题目类型', index: 'question_type_id', width: 80, formatter: function(value, options, row){
					if(value==0)
						return '<span class="label">判断题</span>'
					else	if(value==1)
						return '<span class="label">单选题</span>'
					else	if(value==2)
						return '<span class="label">多选题</span>'
					else	if(value==3)
						return '<span class="label">简答题</span>'
					else
						return '<span class="label">眉头体型</span>'
				} },
			{ label: '', name: 'questionContent', index: 'question_content', width: 80 }, 			
			// { label: '分数', name: 'fraction', index: 'fraction', width: 80 },
			// { label: '课程id', name: 'courseId', index: 'course_id', width: 80 },
			// { label: '', name: 'teacherId', index: 'teacher_id', width: 80 },
			// { label: '章id', name: 'chapterId', index: 'chapter_id', width: 80 },
			// { label: '节id', name: 'sectionId', index: 'section_id', width: 80 },
			{ label: '选项1', name: 'optionOne', index: 'option_one', width: 80 }, 			
			{ label: '选项2', name: 'optionTwo', index: 'option_two', width: 80 }, 			
			{ label: '选项3', name: 'optionThree', index: 'option_three', width: 80 }, 			
			{ label: '选项4', name: 'optionFour', index: 'option_four', width: 80 }, 			
			{ label: '答案', name: 'answer', index: 'answer', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		chooseTest: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.chooseTest = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.chooseTest.id == null ? "myCourse/choosetest/save" : "myCourse/choosetest/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.chooseTest),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "myCourse/choosetest/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "myCourse/choosetest/info/"+id, function(r){

                $("#questonContentImg").attr('src',r.chooseTest.questonContentImg);
                $("#optionOneImg").attr('src',r.chooseTest.optionOneImg);
                $("#optionTwoImg").attr('src',r.chooseTest.optionTwoImg);
                $("#optionThreeImg").attr('src',r.chooseTest.optionThreeImg);
                $("#optionFourImg").attr('src',r.chooseTest.optionFourImg);
                $("#analysisImg").attr('src',r.chooseTest.analysisImg)
                vm.chooseTest = r.chooseTest;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});